package com.zkzx.util.date;


import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

/**
 * @author young Email:yanghui@zkraising.com
 * @description 时间操作工具类
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年04月23日 10时39分
 */
public class DateUtil {

    /**
     * 短日期格式
     */
    public final static String SHORT_DATE_FOR_MATER = "yyyy-MM-dd";

    public final static String SHORT_TIME_FOR= "HH:mm";

    public final static String SHORT_TIME_FOR_MATER = "HH:mm:ss";

    public final static String SHORT_TIME_FOR_TIME = "00:00:00";

    public final static String LONG_DATE_FOR_MATER = "yyyy-MM-dd HH:mm:ss";

    public final static String LONG_DATE_FOR_MATER_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    /**
     * Solr中使用的日期格式
     */
    public static final String SOLR_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:sss'Z'";

    public static final long DAY_TIMESTAMP = 24 * 60 * 60 * 1000L;



    /**
     * 把时间装成指定的字符串 (yyyy-MM-dd HH:mm:ss)
     *
     * @param date
     *            日期
     * @return 时间字符串
     */
    public static final String convertDateToString(Date date) {
        return convertDateToString(LONG_DATE_FOR_MATER, date);
    }

    /**
     *   把字符串时间转 LocalDateTime(yyyy-MM-dd HH:mm:ss)
     * @param date 字符日期
     * @return LocalDateTime
     */
    public static final LocalDateTime convertStringToLocalDateTime(String date){
        try {
            return LocalDateTime.parse(date,DateTimeFormatter.ofPattern(LONG_DATE_FOR_MATER));
        } catch (Exception e) {
            return LocalDate.parse(date,DateTimeFormatter.ofPattern(SHORT_DATE_FOR_MATER)).atStartOfDay();
        }
    }

    /**
     *  字符日期 转 字符日期时间
     * @param date 字符日期
     * @return 字符日期时间
     */
    public static final String convertStringDateToStringDateTime(String date){
        return date.concat(" ").concat(SHORT_TIME_FOR_TIME);
    }

    public static final String convertStringDateTimeToStringDate(String time){
        if (StringUtils.isBlank(time)){
            return null;
        }
       LocalDateTime localDateTime = convertStringToLocalDateTime(time);
        LocalDate localDate = localDateTime.toLocalDate();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(SHORT_DATE_FOR_MATER);
        return localDate.format(fmt);

    }

    /**
     *  LocalDateTime 转 字符日期
     * @param localDateTime
     * @return 字符日期
     */
    public static final String convertLocalDateTimeToString(LocalDateTime localDateTime){
        if (Objects.isNull(localDateTime)){
            return null;
        }
        DateTimeFormatter dtf = DateTimeFormatter.ISO_LOCAL_DATE;
        return localDateTime.format(dtf);
    }


    /**
     * 获取昨天
     * @param date
     * @return 昨天
     */
    public static String getYesterday(Date date) {
        if (Objects.isNull(date)){
            return null;
        }

        Calendar current = Calendar.getInstance();

        Calendar today = Calendar.getInstance();

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH));
        today.set( Calendar.HOUR_OF_DAY, 0);
        today.set( Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        Calendar yesterday = Calendar.getInstance();

        yesterday.set(Calendar.YEAR, current.get(Calendar.YEAR));
        yesterday.set(Calendar.MONTH, current.get(Calendar.MONTH));
        yesterday.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH)-1);
        yesterday.set( Calendar.HOUR_OF_DAY, 0);
        yesterday.set( Calendar.MINUTE, 0);
        yesterday.set(Calendar.SECOND, 0);

        current.setTime(date);

        String time = convertDateToString(date);
        if(current.after(today)){
            return time.split(" ")[1];
        }else if(current.before(today) && current.after(yesterday)){

            return "昨天 "+time.split(" ")[1];
        }else{
            return time.split(" ")[0];
        }
    }

    /**
     * 把时间装成指定的字符串
     *
     * @param datePattern
     *            转换格式
     * @param date
     *            日期
     * @return 时间字符串
     */
    public static final String convertDateToString(String datePattern, Date date) {
        String returnValue = null;
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(datePattern);
            returnValue = df.format(date);
        }
        return (returnValue);
    }

    public static final String getYearLastTwo(){
       return new SimpleDateFormat("yy", Locale.CHINESE).format(Calendar.getInstance().getTime());
    }

    public static final String getMonth(){
        return new SimpleDateFormat("MM", Locale.CHINESE).format(Calendar.getInstance().getTime());
    }

    public static final String getDay(){
        return new SimpleDateFormat("dd", Locale.CHINESE).format(Calendar.getInstance().getTime());
    }



}
